


/* ----------------------------------------------------------------------------------------------------
* 个人中心
* ----------------------------------------------------------------------------------------------------
*/


/**
 * html.html加载
 * 左侧菜单
 */
function memberHandler(){
	$(".subMenu dt").click(function(){
		if ($(this).hasClass("collapse")){
			$(this).removeClass("collapse").nextAll("dd").show();
		}else{
			$(this).addClass("collapse").nextAll("dd").hide();
		}
	});
	$(".subMenu a").click(function(){
		$(".subMenu").children("dd").removeClass("currentMenu");
		$(this).parent().addClass("currentMenu");
	});
}






/**
 * html.html中加载
 * 检查会员登录状态
 */
function inimember(){
	var islogin;
	var mb_id;
	var mb_nickname;
	var mb_key;
	var content;
	islogin=getCookie('islogin');
	
	mb_id=getCookie('mb_id');

	if(!islogin || !mb_id){  //未登录
		
		content=
			'<ul id="loginNav" class="clearfix">'+
			'<li><a id="signin" class="strong" href="javascript:loadloginbox();" title="登录">登录</a></li>'+
			'<li class="separator">|</li>'+
			'<li><h2><a id="signup" href="'+APP+'/User/register" title="免费注册" style="color:#f60;">注册</a></h2></li>'+
			'<li class="separator">|</li>'+
			'<li><h2><a id="lostpw" href="'+APP+'/Article/index/id/7" title="公正竞拍">公正竞拍</a> &amp; <a id="lostpw" href="'+APP+'/Article/index/id/6" title="正品承诺">正品承诺</a></h2></li>'+
			'</ul>'+
			'<script type="text/javascript">client_clear();</script>';
		
		$("#accountInfo").html(content);

	}else{  //已登录
		$.ajax({
			url: APP+"/User/inimember/",
			cache: false,
			type: "POST",
			dataType: "json",
			error: function (XMLHttpRequest, textStatus, errorThrown){logout();},
			success: function(data){
				if(data.newNum>0){  //接收消息
					var newscontent='<li><img src="'+IMG_PATH+'/news.gif" /> <a id="userNews" href="'+APP+'/Member/index/void/mb_message/" title="您有新短信">新短信('+data.newNum+'条)</a></li>';
				}else{
					var newscontent='';
				}
				content=
				'<ul class="clearfix">'+
				'<li>'+
				'	您好，<span>'+data.nickname+'</span>，'+
				'	<a href="'+APP+'/Member" title="到个人中心">个人中心</a>'+
				'	<div id="inviteBuddy">'+
				'		<a href="javascript:copy_code();" title="推荐好友，奖励'+POINTBNAME+'"><img width="52" height="19" alt="推荐好友，奖励'+POINTBNAME+'" src="'+IMG_PATH+'/member/inviteBuddy.gif" /></a>'+
				'	</div>'+
				'</li>'+

				newscontent+

				'<li id="accountBalanceWrap" class="dropMenu">'+
				'<h3 id="checkMyAccount" title="查看账户信息">账户信息<span class="f10">▼</span></h3>'+
				'<div id="accountBalance" class="dropMenuBox" style="width:220px;">'+
				'</div>'+
				'<div class="dropMenuShadow" style="height:100px;"></div>'+
				'</li>'+
				
				'<li>'+
				'	<a href="javascript:logout();" title="安全退出">退出</a>'+
				'</li>'+
				'</ul>';

				
				
				
				$("#accountInfo").html(content);
				
				
				updateuser();   //更新用户状态

				$("#accountBalanceWrap").hover(     //账户信息
					function(){
						var content;
						var isgetPointB=getCookie('isgetPointB');
						var PointB_btn;
						var isgetPointC=getCookie('isgetPointC');
						var PointC_btn;
	
						if(isgetPointB==0){
							PointB_btn = '<a id="getPointB" href="javascript:getPointB();" title="打卡领取'+POINTBNAME+'" class="conutType getPointB"></a>';
						}else{
							PointB_btn = '<a id="gotPointB" href="javascript:void(0);"  class="conutType getPointC"></a>';
						}
	
						/*if(isgetPointC==0){
							PointC_btn = '<a id="getPointC" href="javascript:getPointC();" title="打卡领取" class="conutType getPointC"></a>';
						}else{
							PointC_btn = '<a id="gotPointC"  href="javascript:getPointC();"  class="conutType getPointC"></a>';
						}*/
						
						$("#accountBalance").html(loading);
						$.ajax({
							type: "POST",
							url:APP+"/User/getcount/",   //用户信息
						//	data: "mb_key="+mb_key,
							dataType: "json",
							success: function(data){
								if(data.islogin=='0'){
									logout();//如果服务里转回1刚表示服务器里已没有这个用户的session
								}else{
									content='	<p>'+
											'		<span id="balanceA" class="conutType"></span>'+
											'		<strong class="fl keyword">'+data.a_point+'</strong>'+
											'		<a id="getPointA" href="javascript:paycountnow();" title="充值获取'+BEATNAME+'" class="conutType getPointA"></a>'+
											'	</p>'+
										//	'	<p>'+
										//	'		<span id="balanceB" class="conutType"></span>'+
										//	'		<strong class="fl keyword">'+data.b_point+'</strong>'+
										//	PointB_btn+
										//	'	</p>'+
											'	<p style="margin-bottom:0;">'+
											'		提示：如不了解请到<a href="'+APP+'/Help/index" title="帮助中心">帮助中心</a>查看。'+
											'	</p>';
									$("#accountBalance").html(content);
								}
							}
						});
						
						$("#accountBalanceWrap").addClass("dropMenuOn");
						$("#accountBalance").show();
					},
					
					function(){
						$("#accountBalanceWrap").removeClass("dropMenuOn");
						$("#accountBalance").hide();
					}
					);

			}
		});
	}
}





/**
 * 加载登录信息
 */
function loadloginbox(){
	addDialogbox("#ajaxSignin","登录",10001,502);
	
	$.ajax({
		url: APP+"/User/signin/",
		cache: false,
		success: function(msg){
			$("#ajaxSignin .dialogboxContent").prepend(msg);
			
			/*
			var regMsg = '<p><a href="'+APP+'/User/register"><img src="'+IMG_PATH+'/member/regGetPoint.png" width="450" height="60" alt="现在注册即送300'+POINTBNAME+'" /></a></p>';
			$("#regMsg").attr("style","margin-top:10px;")
			$("#regMsg").prepend(regMsg);
			*/
			
			initStyle();
			
			if($("#userID").attr("value") == ""){
				$("#userID").focus();
			}else{
				$("#userPW").focus();
			}
		}
	});
	
}



/**
 * 更新用户状态
 */
function updateuser() {
	$.ajax({
		type: "POST",
		url:APP+"/User/ajaxcheckuser/",
		success: function(msg){
			if(msg==2){						//证明已经被封了或是没登陆
				//alert("系统检测到您的帐号存在异常,可能是以下几种原因造成的: \r\r 1.因为路由器重新拨号改变了您的ip地址. \r 2.您在其它页面已经退出了登陆状态. \r 3.因为其它原因,你的ip地址产生了变动. \r 4.您的帐号被暂时冻结.\r\r请您不用担心,只要重新登陆就能解决这个问题.\r如果这个状态频繁出现并影响了您正常的竞拍,请发邮件到客服邮箱与我们联系!");
				logout();
			}else{
				setTimeout("updateuser()", 60000);//60S
			}
		}
	});
}



/**
 * 退出
 */
function logout(){
	$.post(APP+"/User/logout",'',function(msg){
		if(msg){//先保留msg这个值以备以后使用
			client_clear();
		}
		location.href=REQUEST_URI;
	});
}



/**
 * 退出 删除cookie
 */
function client_clear(){
	delCookie('islogin');
	delCookie('mb_id');
	delCookie('isgetPointB');
	delCookie('isgetPointC');
}





/**
 * 用户打卡赠送虚拟币ajax 操作
 */
function getPointB(){
	$.post(APP+"/Member/ajaxDoExpoint",{},
		function(data,success){ ///1充值成功 2打过卡 3充值失败 4未通过安全验证
			if(data == 0){
				nbalert("打卡失败","请重新操作！");
			}else if(data == 1){
				//mbsimajax(APP+"/Member/mb_index","mbAjaxShow");
				//$("#getPoint").removeClass("ringIn").addClass("ringOut").attr("title","今天已经打过卡了,请明天再来。");
				nbalert("打卡成功，记得明天还要来哦。","超级大礼打卡送"+POINTBNAME);
			}else if(data == 2){
				nbalert("今天已经打过过卡啦，请明天再来。","超级大礼打卡送"+POINTBNAME);
			}else if(data == 3){
				nbalert("尚未手机验证，打卡失败!","超级大礼打卡送"+POINTBNAME);
			}else{
				nbalert("对不起,您尚未登录!","超级大礼打卡送"+POINTBNAME);
			} 
	});
}


function nbalert(txt,title){
	addDialogbox("#nbalertox",title,10002,402);
	msg=txt+'<br /><br />'+
			'<div class="tc">'+
			'<span class="button">'+
			'<span class="first-child">'+
			'<button type="button" class="f12" onclick="removeDialogbox(\'#nbalertox\');">我知道了，返回</button>'+
			'</span>'+
			'</span>'+
			'</div>';
	$("#nbalertox .dialogboxContent").html(msg);
}


function tlsalert(txt){
	nbalert(txt,"提示");
}








/**
 * 账户充值
 */
function paycountnow(){
//	$.post(APP+"/Member/regcheck",
//	{},
//	function(data){
//		if(data=="1"){
			addDialogbox("#mbpaycountnow",BEATNAME + "充值",10001,768);
			//quitScreenMask();
			$.ajax({
				url: APP+"/Member/mb_count_single/",
				cache: false,
				success: function(msg){
					$("#mbpaycountnow .dialogboxContent").addClass("regWrap");
					$("#mbpaycountnow .dialogboxContent").prepend(msg);
				}
			});
//		}else{
//			addDialogbox("#check_dl","您的"+BEATNAME+"充足，无需充值。",10010,302);
//			msg=''+
//			'<div class="tc">'+
//			'<span class="button">'+
//			'<span class="first-child">'+
//			'<button type="button" class="f12" onclick="check_dl_ok()">我知道了,返回</button>'+
//			'</span>'+
//			'</span>'+
//			'</div>';
//			$("#check_dl .dialogboxContent").html(msg);
//		}
//	});
}

/*
function check_dl_ok(){
	removeDialogbox("#check_dl");
}
*/






/**
 * 登录操作
 */
function ajaxlogin() {
	var exp  = new Date();
	var codehtml='<label for="logincode">验证码:</label>'+'<input type="text" class="input fl space" id="logincode" name="logincode" style="width:60px;" maxlength="4" />'+'<img id="verifyImg_login" class="fl" src="'+APP+'/User/verify/'+exp.getTime()+'" onclick="fleshVerify(\'verifyImg_login\')" title="点击刷新验证码" style="width: 68px; height: 30px;"/>';
	var pass=$.formValidator.pageIsValid('loginform');
	var loginwrongtime=getCookie('loginwrongtime');
	
	if($("#fristloginmark").attr("value") == "F"){
		//_gaq.push(['_trackPageview','/stat_user/fristlogin']);//lee99加入google统计//第一次
	}

	if(pass){
		var queryString = $('#loginform').formSerialize();
		$.post(APP+"/User/ajaxlogin",queryString,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/ajaxlogin']);//lee99加入google统计
			if(msg==1){
				if(loginwrongtime>2){   //登录错误超过3次
					$('#verifycodeInput').removeClass("displayNone");
					$('#verifycodeInput').html(codehtml);
				}
				//_gaq.push(['_trackPageview','/stat_user/ajaxlogin/wrongpwd']);//lee99加入google统计//错误密码
				showtips('邮箱名或密码不正确或者账号已锁定，请确认','tips',3);
			}else if(msg==2){
				$('#verifycodeInput').removeClass("displayNone");
				$('#verifycodeInput').html(codehtml);
				//_gaq.push(['_trackPageview','/stat_user/ajaxlogin/wrongvlidcode']);//lee99加入google统计//错误验证码
				showtips('验证码有误，请重新输入','tips',3);
			}
			else{
				//_gaq.push(['_trackPageview','/stat_user/ajaxlogin/loginpass']);//lee99加入google统计//正常
				dologinurl();
			}
		});
	}
}




/**
 * 注册账户
 */
function addMember() {
	var userEmail=$("#userEmail").val();
	var userverifycode=$("#addMembercode").val();
	var pass=$.formValidator.pageIsValid('addMemberform');
	if(pass){
		$.post(APP+"/User/addmember",'mail='+userEmail+'&code='+userverifycode,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/send_registermail']);//lee99加入google统计
			if(msg=='0'){//先保留msg这个值以备以后使用
				showtips('注册成功，系统已发送验证邮件到您邮箱：'+userEmail+'里，如果5分钟内没有收到邮件请更换注册邮箱或重复发送邮件！','tips',1);
				showresendbtn();//让用户可以再次发送邮件
			}else{
				showtips(msg,'tips',2);
			}
		});
	}
}




/**
 * 已有相同用户名的处理方式
 */
function showresendbtn() {
	$('#sendbtn').attr("style","display:none");
	$('#resendbtn').removeAttr("style");
}


/**
 * 已有相同用户名的处理方式
 */
function hideresendbtn() {
	$('#resendbtn').attr("style","display:none");
	$('#sendbtn').removeAttr("style");
}



/**
 * 重新发送邮件
 */
function resendmail() {
	var userEmail=$("#userEmail").val();
	var userverifycode=$("#addMembercode").val();
	var pass=$.formValidator.pageIsValid('addMemberform');
	if(pass){
		$.post(APP+"/User/resendmail",'mail='+userEmail+'&code='+userverifycode,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/resend_registermail']);//lee99加入google统计
			if(msg=='0'){//先保留msg这个值以备以后使用
				showtips('系统已重新发送验证邮件到您邮箱：'+userEmail+'里，如果5分钟内没有收到邮件请更换注册邮箱或重复发送邮件！','tips',1);
			}else{
				showtips(msg,'tips',2);
			}
		});
	}
}



/**
 * 找回密码
 */
function ajaxgetpwd() {
	var pass=$.formValidator.pageIsValid('getpwdform');

	if(pass){
		var queryString = $('#getpwdform').formSerialize();
		$.post(APP+"/User/getpwd",queryString,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/send_resetpwd']);//lee99加入google统计
			if(msg=='0'){//先保留msg这个值以备以后使用
				showtips('操作成功，系统已发送邮件到您邮箱','tips',1);
			}else if(msg=='3'){//先保留msg这个值以备以后使用
				showtips('找回密码的功能每天只能用3次,请您稍后再试','tips',1);
			}else{
				showtips(msg,'tips',2);
			}
		});
	}
}






/**
 * 修改个性图片
 * 用户选择自己的图像
 */
function usericon(){
	addDialogbox("#usericon","选择自己的个性图像",10001,502);
	//quitScreenMask();
	loadUserIcon();
}

/**
 * 选择图像
 */
function loadUserIcon(){
	$.ajax({
		url: APP+"/User/memberlogo/",
		cache: false,
		success: function(msg){
			$("#usericon .dialogboxContent").html(msg);
			$("#newIconList button").click(function(){
				loadUserIcon();
			});
		}
	});
}


/**
 * 用户选择自己的图像后操作
 * @param path
 */
function selsectlogo(path){
	$('#showicon').attr('src',path);
	$('#photo').attr('value',path);
	removeDialogbox("#usericon");
}












/**
 * 
 * 判断是否在登陆页还是用ajax的形式登陆
 */
function dologinurl(){
	if(URL =='/User'){
		location.href=APP+'/Member/';
	}else{
		location.href=REQUEST_URI;
	}
}






/**
 * 订单选择用户地址薄
 */
function userAddress(){
	addDialogbox("#userAddress","选择您的地址",10001,502);
	$.ajax({
		url: APP+"/Member/userAddress/",
		cache: false,
		success: function(msg){
			$("#userAddress .dialogboxContent").html(msg);
			$("#userAddress .dialogboxContent").addClass("regWrap");
		}
	});
}



/**
 * 订单保存用户所选地址薄
 * @param id
 */
function selectaddr(id){
	$("#userRealName").val(
		$("#userRealName_"+id).text()
	);
	$("#userPhoneNumber").val(
		$("#userPhoneNumber_"+id).text()
	);
	$("#detailedAddress").val(
		$("#detailedAddress_"+id).text()
	);
	$("#zipCode").val(
		$("#zipCode_"+id).text()
	);
	$("#remarks").val(
		$("#remarks_"+id).text()
	);
	removeDialogbox("#userAddress");
}



/**
 * 加载订单页面
 * ajax_mallorder.html
 * @param oid
 */
function loadOrder(oid){
	addDialogbox("#order","订单",10001,860);
	var orderFrame = '<iframe id="orderFrame" onload="iframeAutoHeight(\'orderFrame\')" name="orderFrame" width="100%" scrolling="no" frameborder="no" allowtransparency="yes" src='+APP+'/Member/showOrder/id/'+oid+'></iframe>';
	$("#order .dialogboxContent").html(orderFrame);
}







/**
 * 
 * 首页导航留言
 */
function saveMessage(){
	var islogin;
	var mb_id;
	islogin=getCookie('islogin');  //是否登录
	mb_id=getCookie('mb_id');     //会员ID
	
	if(!islogin || !mb_id){
		loadloginbox();
	}else{
		var queryString = $('#messageForm').formSerialize();
		$.post(APP+"/Member/saveMessage",queryString,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/send_resetpwd']);//lee99加入google统计
			if(msg=='1' || msg=='0'){//先保留msg这个值以备以后使用
				location.href=APP+"/Message/index";
			}else{
				loadloginbox();
			}
		});
	}
	
}


/**
 * 用户留言列表中留言
 */
function saveMessage2(){
	var islogin;
	var mb_id;
	islogin=getCookie('islogin');
	mb_id=getCookie('mb_id');
	
	if(!islogin || !mb_id){
		loadloginbox();
	}else{
		var queryString = $('#messageForm2').formSerialize();
		$.post(APP+"/Member/saveMessage",queryString,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/send_resetpwd']);//lee99加入google统计
			if(msg=='1'){//先保留msg这个值以备以后使用
				nbalert('留言成功','tips',1);
				location.href=APP+"/Message/index";
			}else if(msg=='0'){
				nbalert('留言失败','tips',1);
			}else{
				loadloginbox();
			}
		});
	}
}







